package models

import (
	"github.com/astaxie/beego/orm"
	"strconv"
	"strings"
)

type AdminGroupModels struct {
}

type AdminGroup struct {
	Id       int64
	Admin_id int64
	Group_id int64
}

func init() {
	orm.RegisterModel(new(AdminGroup))
}

func (m *AdminGroupModels) Update(maps map[string]interface{}) (int64, error) {
	gi := strings.Split(maps["group_id"].(string), ",")
	ag := AdminGroup{}
	ag.Admin_id = maps["admin_id"].(int64)
	//清除
	_, err := m.Delete(ag.Admin_id)
	if err != nil {
		return 0, err
	}
	o := orm.NewOrm()
	var row int64
	for i := 0; i < len(gi); i++ {
		row, _ = strconv.ParseInt(gi[i], 10, 64)
		row, err = o.Insert(&AdminGroup{Admin_id: maps["admin_id"].(int64), Group_id: row})
		if err != nil {
			return 0, err
		}
	}
	return row, err
}

//删除admin_id
func (m *AdminGroupModels) Delete(aid int64) (int64, error) {
	return orm.NewOrm().Delete(&AdminGroup{Admin_id: aid}, "admin_id")
}

// 获取
func (m *AdminGroupModels) Query(aid int64) ([]AdminGroup, error) {
	var list []AdminGroup
	ps := getQueryTable("admin_group")
	_, err := ps.Filter("admin_id", aid).All(&list)
	return list, err
}
